using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource.Analysis;
using UCL.Powersource.Helpers;
using UCL.Powersource.Statistics;
using UCL.Powersource.ViewModels.Analysis;

namespace UCL.Powersource.ViewModels.AnalysisBase
{
    public abstract class OneTeamOverTimeStatisticsViewModelBase : StatisticViewModelBase
    {
        public OneTeamOverTimeStatisticsViewModelBase(bool usesNormalizedValues)
            : base(StatisticViewModelTypes.SingleTeam_MultipleTimeFrames, true, false, usesNormalizedValues)
        { }

        protected override void Recalculate_Internal()
        {
            StatisticResultsTable_Recalculate();
        }

        protected override bool OnActivating()
        {
            StatisticResultsTable_Recalculate();
            return true;
        }

        protected override bool OnDeactivating()
        {
            return true;
        }


        protected override void OnActiveTfsTeamProjectConfigurationChanged()
        {
            base.OnActiveTfsTeamProjectConfigurationChanged();
            StatisticResultsTable_Recalculate();
        }

        protected override void OnTimeFramesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            base.OnTimeFramesCollectionChanged(sender, e);
            StatisticResultsTable_Recalculate();
        }

        protected override void OnStatisticsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            base.OnStatisticsCollectionChanged(sender, e);
            StatisticResultsTable_Recalculate();
        }

        protected override void OnShowNormalizedChanged()
        {
            base.OnShowNormalizedChanged();
            SetActiveStatisticResultsTable();
        }


        public override bool CanRegisterStatistic(IStatistic statistic)
        {
            return true;
        }

        protected virtual void StatisticResultsTable_Recalculate()
        {
            if (ActiveIReportingContext == null ||
                ActiveIReportingContext.ActiveTfsTeamProjectConfiguration == null ||
                ActiveIReportingContext.SelectedIStatistics == null ||
                ActiveIReportingContext.SelectedITimeFrames == null)
            {
                var table = new DataTable();
                StatisticResultsTableSet = new Tuple<DataTable, DataTable>(table, table);
            }
            else
            {
                StatisticResultsTableSet = DataTableBuilder.GetTableForActiveTeam(ActiveIReportingContext.ActiveTfsTeamProjectConfiguration,
                    ActiveIReportingContext.SelectedITimeFrames, ActiveIReportingContext.SelectedIStatistics, IsConsumingShowNormalized);
            }

            SetActiveStatisticResultsTable();
        }
    }
}
